﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.NetworkInformation;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace ManhattanProcedure.Model
{
    public class PwebSockets
    {
        private static PwebSockets instance;
        private PwebSockets() { }
        public static PwebSockets Instance
        {
            get
            {
                if (instance == null)
                {
                    instance = new PwebSockets();
                }
                return instance;
            }
        }
        /// <summary>
        /// 其他东西的通讯返回数据
        /// </summary>
        private List<byte> buffer = new List<byte>();
        /// <summary>
        /// 是否可以打印
        /// </summary>
        public static bool isPrinter = false;
        public static bool isPrinterOver = false;
        /// <summary>
        /// 收到消息
        /// </summary>
        public static bool IsReviecePrinter = false;
        /// <summary>
        /// 错误信息
        /// </summary>
        public static string ErrorMsg = "";
        public static bool isPrinterSocket = false;
      //  public static PrinterWebSocket pSocketClient = null;// new PrinterWebSocket("ws://" + IPorPortInfo.Instance.PrinterCoffeeIP + ":" + IPorPortInfo.Instance.PrinterCofferPort);

        /// <summary>
        /// PING 通 IP
        /// </summary>
        /// <param name="IP"></param>
        /// <returns></returns>
        public bool PingIP(string IP)
        {
            try
            {
                if (IP.Split('.').Count() < 4) return false;
                Ping pingSender = new Ping();
                PingReply reply = pingSender.Send(IP, 120);//第一个参数为ip地址，第二个参数为ping的时间
                if (reply.Status == IPStatus.Success)
                {
                    return true;
                }
                else
                {
                    //ping不通
                    return false;
                }
            }
            catch (Exception)
            {
                return false;
            }
        }
        public void Printer_Thr()
        {
            try
            {
                if (!PingIP(IPorPortInfo.Instance.PrinterCoffeeIP))
                {
                    Thread.Sleep(100);
                    Printer_Thr();
                    return;
                }
               // Pwebsocket();
                //  Heart();
            }
            catch (Exception)
            {
                Printer_Thr();
            }
        }
        public void DyCX()
        {
            try
            {
                using (var cvt = new JmPrint.Converter(JmPrint.PrinterType.Color, "revert=0:duplex=120"))
                {
                    using (var usbmgr = new USB.Manager())
                    {
                        var dd = usbmgr.Open(0);
                        try
                        {
                            var dd3 = usbmgr.GetStatus2(0xF4, 4);
                            Console.WriteLine(dd3.Count());
                            if (dd3.Count() > 3)
                            {
                                var msg = dd3[0] + " " + dd3[1] + " " + dd3[2] + " " + dd3[3];
                                Console.WriteLine(dd3[0] + " " + dd3[1] + " " + dd3[2] + " " + dd3[3]);
                                if (msg == "8 0 0 0" || msg == "9 0 0 0")
                                {
                                    WuLiaoCoffeeSel.Instance.Print_Bol = true;
                                }
                                else
                                {
                                    WuLiaoCoffeeSel.Instance.Print_Bol = false;
                                }
                            }
                        }
                        catch (Exception)
                        {
                        }
                        usbmgr.Close();
                        Console.WriteLine("ending");
                    }

                }
            }
            catch (Exception)
            {

            }
        }
        public int Init = 0;
        public void DyCS(string path)
        {
            try
            {
                using (var cvt = new JmPrint.Converter(JmPrint.PrinterType.Color, "revert=0:duplex=120"))
                {
                    var imgPath = path;
                    cvt.ConvertImage(imgPath, 600, 600);
                    var data = cvt.GetData();
                    using (var usbmgr = new USB.Manager())
                    {
                        try
                        {
                            var dd = usbmgr.Open(0);
                            var dd2 = usbmgr.Write(data);
                        }
                        catch (Exception)
                        {
                        }
                        try
                        {
                            Thread.Sleep(15000);
                            while (true)
                            {
                                var dd3 = usbmgr.GetStatus2(0xF4, 4);
                                Console.WriteLine(dd3.Count());
                                if (dd3.Count() > 3)
                                {
                                    var msg = dd3[0] + " " + dd3[1] + " " + dd3[2] + " " + dd3[3];
                                    Console.WriteLine(dd3[0] + " " + dd3[1] + " " + dd3[2] + " " + dd3[3]);
                                    if (msg == "8 0 0 0" || msg == "9 0 0 0")
                                    {
                                        break;
                                    }
                                }
                                Init++;
                                if (Init == 60)
                                {
                                    break;
                                }
                                Thread.Sleep(1000);
                            }
                        }
                        catch (Exception)
                        {
                            Thread.Sleep(15000);
                        }
                        usbmgr.Close();
                        Thread.Sleep(4000);
                        Console.WriteLine("ending");
                    }

                }
            }
            catch (Exception)
            {
            }
        }
    }
}
